import tkinter as tk  # 导入 Tkinter 库

rootWindow = tk.Tk()
rootWindow.title("窗口标题")
# rootWindow.geometry("600x400")  # 窗口大小

button = tk.Button(rootWindow, text="我是一个按钮", command=lambda: print(myList.curselection()))
button.pack()

# 文本输入框entry
entry = tk.Entry(rootWindow)
entry.pack()

# 列表Listbox
myList = tk.Listbox(rootWindow)
myList.insert('end', "first")  # 'end'是插入方式，在尾部插入
myList.insert('end', "second")
myList.pack()
#  myList.curselection()：list中当前选中的项
#  myList.get(1) list中的第一个项目

# 单选按钮 Radiobutton
tkStr = tk.StringVar()


def printSelection():
    print(tkStr.get())


label = tk.Label(rootWindow, textvar=tkStr)
label.pack()

# 当点击这个单选按钮，会把value的值赋给var
radioBtn = tk.Radiobutton(rootWindow, text="选项A", var=tkStr, value="选项A的值", command=printSelection)
radioBtn.pack()


# 横竖向拖动条 scale

def printScaleValue(value):
    print("横向拖动条的值" + value)


# length单位是像素
# tickinterval 隔多少显示一个标尺
s = tk.Scale(rootWindow, label='拖动我', from_=5, to=11, orient=tk.HORIZONTAL,
             length=300, showvalue=0, tickinterval=3, resolution=0.01, command=printScaleValue)
s.pack()

# 多选按钮 CheckBtn
varInt = tk.IntVar()


def onSelect():
    print("多选按钮值：" + str(varInt.get()))


checkBtn = tk.Checkbutton(rootWindow, text="多选按钮1", command=onSelect, variable=varInt,
                          onvalue=1, offvalue=0)
checkBtn.pack()

#  菜单条 Menu
menubar = tk.Menu(rootWindow)
filemenu = tk.Menu(menubar)
menubar.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New")
filemenu.add_command(label="Open")
filemenu.add_command(label="Save")

rootWindow.config(menu=menubar)


rootWindow.mainloop()

# 弹框 MessageBox